
#include <fstream>
#include <string>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "gps.pb.h"
#include "gps_proto_util.h"
#include "gpx_parser.h"
#include "kml/base/math_util.h"

DEFINE_string(ingpx, "", "file.gpx");

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);
  google::ParseCommandLineFlags(&argc, &argv, false);
  if (FLAGS_ingpx.empty()) {
    LOG(ERROR) << "missing arg --ingpx file.gpx";
    return 1;
  }
  gps_proto::GpsProto gps_proto;
  if (!gps_proto::GpxParser::ParseFromFile(FLAGS_ingpx.c_str(), &gps_proto)) {
    LOG(ERROR) << "parse failed: " << FLAGS_ingpx;
    return 1;
  }

  double ascent = 0;
  double descent = 0;
  double dist = 0;
  LOG(INFO) << "<track> size " << gps_proto.track_size();
  for (int t = 0; t < gps_proto.track_size(); ++t) {
    LOG(INFO) << "<segment> size " << gps_proto.track(t).segment_size();
    for (int s = 0; s < gps_proto.track(t).segment_size(); ++s) {
      dist += gps_proto::TotalSegmentDistance(gps_proto.track(t).segment(s),
                                              &ascent, &descent);
    }
  }
  LOG(INFO) << "dist " << dist;
  LOG(INFO) << "ascent " << ascent;
  LOG(INFO) << "descent " << descent;
  return 0;
}

